package com.lzz.bottomencapsulation.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/***
 @package ${package}
 @filename ${数据库操作类}
 */
public class DBHelper extends SQLiteOpenHelper {

    // 数据库文件名
    public static final String DB_NAME = "manager_terminal_database.db";
    // 数据库表名
    public static final String TABLE_NAME_SEARCH_SafetyOfficer = "search_safety_officer";//搜索安全员
    public static final String TABLE_NAME_SEARCH_car = "search_car";//搜索车辆
    public static final String TABLE_NAME_SEARCH_car2 = "search_car_type";//搜索车辆
    public static final String TABLE_NAME_SEARCH_car_eye = "search_car_type_eye";
    public static final String TABLE_NAME_SEARCH_car_follow = "search_car_type_follow";
    public static final String TABLE_NAME_SEARCH_carstate = "search_carstate";//根据状态搜索车辆
    // 数据库版本号
    public static final int DB_VERSION = 3;
    //    创建表语句
    String create_table_SEARCH_SafetyOfficer = "create table " +
            TABLE_NAME_SEARCH_SafetyOfficer +
            "(_id integer primary key autoincrement, content varchar)";
    String create_table_SEARCH_car = "create table " +
            TABLE_NAME_SEARCH_car +
            "(_id integer primary key autoincrement, content varchar)";

    String create_table_SEARCH_car2 = "create table " +
            TABLE_NAME_SEARCH_car2 +
            "(_id integer primary key autoincrement, content varchar, type varchar)";

    String create_table_SEARCH_car_eye = "create table " +
            TABLE_NAME_SEARCH_car_eye +
            "(_id integer primary key autoincrement, content varchar, type varchar)";

    String create_table_SEARCH_car_follow = "create table " +
            TABLE_NAME_SEARCH_car_follow +
            "(_id integer primary key autoincrement, content varchar, type varchar)";

    String create_table_SEARCH_carstate = "create table " +
            TABLE_NAME_SEARCH_carstate +
            "(_id integer primary key autoincrement, content varchar)";

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    // 当数据库文件创建时，执行初始化操作，并且只执行一次
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 建表
        db.execSQL(create_table_SEARCH_SafetyOfficer);
        db.execSQL(create_table_SEARCH_car);
        db.execSQL(create_table_SEARCH_carstate);
        db.execSQL(create_table_SEARCH_car2);
        db.execSQL(create_table_SEARCH_car_eye);
        db.execSQL(create_table_SEARCH_car_follow);
    }

    // 当数据库版本更新执行该方法
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1) {
            db.execSQL(create_table_SEARCH_car2);
        }
        if(oldVersion == 2){
            db.execSQL(create_table_SEARCH_car_eye);
            db.execSQL(create_table_SEARCH_car_follow);
        }

    }
}
